package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.TbDiagnosisInfo;
import com.ruoyi.system.response.BaseResponse;
import com.ruoyi.system.response.StatusCode;
import com.ruoyi.system.service.ITbDiagnosisInfoService;
import com.ruoyi.system.util.QRCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;

import static com.ruoyi.system.controller.TbDiagnosisInfoController.convertImageToBase64Str;

@Api("诊断信息")
@Controller
@ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户编号", dataType = "String", dataTypeClass = String.class)
})
@RequestMapping("/system/qrcode")
public class QrcodeGainController  extends BaseController {

    @Autowired
    private ITbDiagnosisInfoService tbDiagnosisInfoService;

    @ApiOperation("获取二维码")
    @Log(title = "二维码")
    @GetMapping("/query/{userId}")
    @ResponseBody
    //生成二维码并将其返回给前端调用者
    public String generateV2(@PathVariable("userId") String userId, HttpServletResponse servletResponse){

        //根据流水查询url
        TbDiagnosisInfo tbDiagnosisInfo = tbDiagnosisInfoService.selectTbDiagnosisInfoByUserID(userId);
        String content = "http://localhost:80/common/download?fileName="+tbDiagnosisInfo.getQrCodeUrl()+"&delete=false";
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try {
//            QRCodeUtil.createCodeToFile(content, new File("E:\\LineChart"), "123.png");
            QRCodeUtil.createCodeToFile(content, new File(RuoYiConfig.getDownloadPath()), userId+"qrCode.png");


        }catch (Exception e){
            response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
//        return "data:image/png;base64,"+convertImageToBase64Str("E:\\LineChart\\123.png");
        return "data:image/png;base64,"+convertImageToBase64Str(RuoYiConfig.getDownloadPath()+userId+"qrCode.png");
    }

}
